package com.aliyun.heiyu.powermaster.server.strategy.analyzer;

import com.aliyun.heiyu.common.util.LogUtils;
import com.aliyun.heiyu.common.util.StringUtil;
import com.aliyun.heiyu.powermaster.proto.PowerStrategy;
import com.aliyun.heiyu.powermaster.proto.StrategyAction;
import com.aliyun.heiyu.powermaster.server.MasterConstant;
import com.aliyun.heiyu.powermaster.server.manager.ThunderManager;
import com.aliyun.heiyu.powermaster.server.manager.ThunderManagerImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class AICappingStrategyAnalyzer extends StrategyAnalyzer {
    private static Logger logger = LoggerFactory.getLogger(AICappingStrategyAnalyzer.class);

    private ThunderManager thunderManager;

    public AICappingStrategyAnalyzer(){
        super(MasterConstant.AI_CAPPING, PowerStrategy.CAPPING_POWER.name());

        this.thunderManager = new ThunderManagerImpl();
    }

    /**capping动作的触发条件：实际监控值, cupUsage、power满足条件时触发*/
    @Override
    public Boolean monitorCondition(StrategyAction strategyAction, String sn, String level) {
        logger.debug("监控设备  == "+ LogUtils.vaildlog(sn)+"aicapping条件");
        if("3".equals(level)){
            return cappingJudge(sn);
        }else{
            return statusJudge(sn);
        }
    }

    @Override
    protected Boolean isTrue(String sn) {
        return thunderManager.getStatusOfCapping(sn);
    }

    private Boolean statusJudge(String sn){
        Boolean status = thunderManager.getStatusOfCapping(sn);
        if(null != status && status){
            return false;
        }
        return null;
    }

    private Boolean cappingJudge(String sn){
        Boolean status = thunderManager.getStatusOfCapping(sn);
        if(status == null){
            return null;
        }
        Boolean b1 = null;
        Boolean c1 = null;
        Boolean b2 = null;
        Boolean c2 = null;
        String powerUsage = monitor.getPowerUsage(sn, 0);
        if(!StringUtil.isEmpty(powerUsage)){
            b1 = Double.parseDouble(powerUsage) >= MAX_POWER_USAGE;
            if(!b1){
                c1 = Double.parseDouble(powerUsage) < MIN_POWER_USAGE;
            }
        }
        String powerUsage1 = monitor.getPowerUsage(sn, -1);
        if(!StringUtil.isEmpty(powerUsage1)){
            b2 = Double.parseDouble(powerUsage1) >= MAX_POWER_USAGE;
            if(!b2){
                c2 = Double.parseDouble(powerUsage1) < MIN_POWER_USAGE;
            }
        }

        if(b1 == null || b2 == null){
            logger.debug("有条件为 NULL， ai-capping 不进行动作");
            return null;
        }

        if(b1 && b2){
            logger.debug("条件满足， ai-capping 进行动作");
            if(status == null || !status){
                return Boolean.TRUE;
            }
            return null;
        }

        if(c1 == null || c2 == null){
            logger.debug("有条件为 NULL， ai-capping 不进行动作");
            return null;
        }

        if(c1 && c2){
            logger.debug("条件满足， ai-uncapping 进行动作");
            if(status == null || status){
                return Boolean.FALSE;
            }
            return null;
        } else {
            logger.debug("条件满足， 不动作");
            return null;
        }
    }
}
